<?php

declare(strict_types=1);

namespace DreamCat\ObjectValidTest\Cases;

use DreamCat\ObjectValid\Exception\CheckErrorException;
use DreamCat\ObjectValid\Validor\Validator;
use DreamCat\ObjectValidTest\Helper\DemoPojo;
use JetBrains\PhpStorm\Pure;
use PHPUnit\Framework\TestCase;

/**
 * -
 * @author vijay
 */
class ValidatorTest extends TestCase
{
    public function testNormal()
    {
        $pojo = DemoPojo::builder([
            "value" => 50,
            "str" => "aabce",
            "ary" => [
                3,
                5,
                5,
                3,
            ],
        ]);
        $validator = new Validator();
        $validator->validObject($pojo);
        self::assertEquals($pojo, $pojo);
    }

    /**
     * -
     * @return void
     * @dataProvider errorData
     */
    public function testError(object $obj, string $expectedError)
    {
        $this->expectException(CheckErrorException::class);
        $this->expectErrorMessage($expectedError);

        $validator = new Validator();
        $validator->validObject($obj);
    }

    #[Pure]
    public function errorData(): array
    {
        return [
            [
                DemoPojo::builder(["value" => 100]),
                DemoPojo::class . "::value 不能大于 55 且不能小于 15",
            ],
            [
                DemoPojo::builder(["value" => 1]),
                DemoPojo::class . "::value 不能大于 55 且不能小于 15",
            ],
            [
                DemoPojo::builder(["withMsgInt" => 100]),
                "太大了",
            ],
            [
                DemoPojo::builder(["str" => "c"]),
                DemoPojo::class . "::str 不匹配要求的正则表达式 #a#",
            ],
            [
                DemoPojo::builder(["withMsgStr" => "c"]),
                "bbc",
            ],
            [
                DemoPojo::builder(["withMsgStr" => "a"]),
                "aa",
            ],
            [
                DemoPojo::builder(["withMsgStr" => "aacerqwer"]),
                "aa",
            ],
            [
                DemoPojo::builder(
                    [
                        "ary" => [
                            1,
                            2,
                            3,
                            4,
                            5,
                            6,
                            7,
                            8,
                        ],
                    ]
                ),
                DemoPojo::class . "::ary 的 size 不能大于 5 且不能小于 1",
            ],
            [
                DemoPojo::builder(
                    [
                        "ary" => [
                            DemoPojo::builder(["value" => 1]),
                        ],
                    ]
                ),
                DemoPojo::class . "::value 不能大于 55 且不能小于 15",
            ],
        ];
    }
}

# end of file
